﻿using DevExpress.XtraBars.Docking;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CXW.Core.Sharp.Gui
{
    class DockPanelExt : DockPanel
    {
        Codon codon = null;
        /// <summary>显示内容根据此属性获取资源文件</summary>
        public string Language { get; private set; }

        List<NavBarGroupExt> _groups = new List<NavBarGroupExt>();
        /// <summary>返回 ICollection&lt;NavBarGroupExt&gt; 只读集合</summary>
        public ICollection<NavBarGroupExt> Groups { get { return _groups.AsReadOnly(); } }
        /// <summary>返回 NavBarControl 对象</summary>
        public NavBarControl NavBarControl { get; private set; }
        TreeView _treeView = null;
        TreeList _treeList = null;

        public DockPanelExt(Codon codon)
        {
            if (ControlContainer == null) { Controls.Add(new ControlContainer()); }

            Dock = DockingStyle.Fill;
            ID = Guid.NewGuid();

            this.codon = codon;
            Name = codon.Id;
            Tag = codon;
            Language = codon.Properties["language"];

            UpdateText();
        }

        /// <summary>添加分组</summary>
        /// <param name="navBarGroupExt"></param>
        /// <param name="treeView"></param>
        public void AddGroups(NavBarGroupExt navBarGroupExt, TreeView treeView)
        {
            if (navBarGroupExt.ControlContainer != null) { throw new Exception("The same NavBarGroupExt control AddGroups method can only be called once ."); }
            if (NavBarControl == null)
            {
                NavBarControl = new NavBarControl()
                {
                    Dock = DockStyle.Fill,
                    NavigationPaneOverflowPanelUseSmallImages = false,
                    PaintStyleKind = NavBarViewKind.NavigationPane
                };
                NavBarControl.OptionsNavPane.ExpandedWidth = 191;
                NavBarControl.OptionsNavPane.ShowExpandButton = false;

                ControlContainer.Controls.Add(NavBarControl);

                NavBarControl.ActiveGroupChanged += (sender, e) => { ActiveGroupChanged?.Invoke(sender, e); };
            }
            //如果先加载 navBarGroupExt 的内部控件则需要增加  NavBarControl.Controls.Add(nbgcc); 否则内部控件不会显示
            NavBarControl.Groups.Add(navBarGroupExt);
            _groups.Add(navBarGroupExt);

            NavBarGroupControlContainer nbgcc = new NavBarGroupControlContainer();

            nbgcc.Controls.Add(treeView);

            navBarGroupExt.ControlContainer = nbgcc;
            navBarGroupExt.TreeView = treeView;
        }
        /// <summary>添加分组</summary>
        /// <param name="navBarGroupExt"></param>
        /// <param name="treeList"></param>
        public void AddGroups(NavBarGroupExt navBarGroupExt, TreeList treeList)
        {
            if (navBarGroupExt.ControlContainer != null) { throw new Exception("The same NavBarGroupExt control AddGroups method can only be called once ."); }
            if (NavBarControl == null)
            {
                NavBarControl = new NavBarControl()
                {
                    Dock = DockStyle.Fill,
                    NavigationPaneOverflowPanelUseSmallImages = false,
                    PaintStyleKind = NavBarViewKind.NavigationPane
                };
                NavBarControl.OptionsNavPane.ExpandedWidth = 191;
                NavBarControl.OptionsNavPane.ShowExpandButton = false;

                ControlContainer.Controls.Add(NavBarControl);

                NavBarControl.ActiveGroupChanged += (sender, e) => { ActiveGroupChanged?.Invoke(sender, e); };
            }
            //如果先加载 navBarGroupExt 的内部控件则需要增加  NavBarControl.Controls.Add(nbgcc); 否则内部控件不会显示
            NavBarControl.Groups.Add(navBarGroupExt);
            _groups.Add(navBarGroupExt);

            NavBarGroupControlContainer nbgcc = new NavBarGroupControlContainer();

            nbgcc.Controls.Add(treeList);

            navBarGroupExt.ControlContainer = nbgcc;
            navBarGroupExt.TreeList = treeList;
        }

        internal void AddTreeView(TreeView treeView)
        {
            if (Groups.Count > 0) { throw new Exception("Groups.Count >0 时,不允许调用此方法."); }
            if (_treeView != null) { ControlContainer.Controls.Remove(_treeView); }
            _treeView = treeView;
            ControlContainer.Controls.Add(_treeView);
        }
        internal void AddTreeList(TreeList treeList)
        {
            if (Groups.Count > 0) { throw new Exception("Groups.Count >0 时,不允许调用此方法."); }
            if (_treeList != null) { ControlContainer.Controls.Remove(_treeList); }
            _treeList = treeList;
            ControlContainer.Controls.Add(_treeList);
        }

        public event NavBarGroupEventHandler ActiveGroupChanged;
        /// <summary>
        /// NavBarControl 为 null 时,返回 TreeView ; NavBarControl.ActiveGroup is NavBarGroupExt 为true时,返回 NavBarGroupExt.TreeView;其他情形返回 null
        /// </summary>
        public TreeView ActiveTreeView
        {
            get
            {
                return NavBarControl == null ? _treeView :
                  (NavBarControl.ActiveGroup is NavBarGroupExt ? (NavBarControl.ActiveGroup as NavBarGroupExt).TreeView : null);
            }
        }
        /// <summary>
        /// NavBarControl 为 null 时,返回 TreeList ; NavBarControl.ActiveGroup is NavBarGroupExt 为true时,返回 NavBarGroupExt.TreeList;其他情形返回 null
        /// </summary>
        public TreeList ActiveTreeList
        {
            get
            {
                return NavBarControl == null ? _treeList :
                  (NavBarControl.ActiveGroup is NavBarGroupExt ? (NavBarControl.ActiveGroup as NavBarGroupExt).TreeList : null);
            }
        }

        string ResourceServiceLanguage = "";
        /// <summary>更新UI显示文字</summary>
        public void UpdateText()
        {
            if (ResourceServiceLanguage != ResourceService.Language)
            {
                LoggingService.DebugFormatted("1 Name = {1},Text = {0}", Text, Name);
                ResourceServiceLanguage = ResourceService.Language;
                Text = LanguageHelper.GetText(codon);
                LoggingService.DebugFormatted("--2 Name = {1},Text = {0}", Text, Name);
            }
        }
    }
}
